<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet"   th:href="@{lib/layui-v2.5.5/css/layui.css}"   media="all">
    <link rel="stylesheet"  th:href="@{css/public.css}"   media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheakData">添加菜单</button>
    </div>
</script>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    {{# if (d.type=='2'){ }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# } }}
</script>

<script th:src="@{lib/layui-v2.5.5/layui.js}"  charset="utf-8"></script>
<script  th:src="@{js/lay-config.js?v=1.0.4}" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'authorityId',
            treePidName: 'parentId',
            elem: '#munu-table',
            url: 'menu/menuAll',
            treeDefaultClose:true,//默认是一个折叠的树
            toolbar:'#toolbarDemo',
            page: false,
            cols: [
                [
                {type: 'numbers'},
                {field: 'name', width: 240, title: '菜单名称'},
                {field: 'type', title: '类型',width:100,
                    templet:function(res){
                       if(res.type=='0'){//类型。0：目录；1：菜单；2：按钮；3：导航菜单
                           return '<span class="layui-badge layui-bg-blue">目录</span>';
                       }else if(res.type=='1'){
                           return '<span class="layui-badge-rim">菜单</span>';
                       }else if(res.type=='2'){
                           return '<span class="layui-badge layui-bg-gray">按钮</span>';
                       }else{
                           return '<span class="layui-badge ">导航菜单</span>';
                       }
                    }
                },
                {field: 'url', title: 'URL',width:200},
                {field: 'permission', width: 200, align: 'center', title: '权限标识'},
                {field: 'sort', width: 100, align: 'center', title: '排序号'},
                {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
            ]
            ],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        /**
         * 添加功能js函数
         */
        function add(content){
            //添加操作
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['50%', '50%'],
                content: content
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        };

        /**
         * 监听事件
         */
        table.on('toolbar(munu-table)',function (obj){
            if(obj.event==='getCheakData'){
                // alert("添加内容");
                add('addMenu?type=-1&parentId=-1');
            }

        } );

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {
                layer.confirm('真的要删除吗？',function(index){
                    //直接向服务器发送删除指令
                    $.ajax({
                        url:'menu/deleteMenuById',
                        type:'post',
                        data:{id:data.id},
                        success:function(suc){
                            if(suc.code==200){
                                layer.close(index);
                                layer.msg("删除成功！");
                                parent.window.location.reload();
                            }else{
                                layer.msg("删除失败!!!");
                            }
                        }
                    })

                })

            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '修改菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '50%'],
                    //1 获取当前行的信息渲染到弹出层form单上 2、通过点击记录的id查询数据信息然后进行渲染
                    content: 'menu/queryMenuByID?id='+data.id
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

            }else{
                add('addMenu?type='+data.type+'&parentId='+data.id);
            }
        });
    });
</script>
</body>
</html>